<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Result;
use think\Request;
use app\api\model\Users;
// 使用think自带异常类抛出异常
use app\ExceptionHandle;

class Indexx
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function indexx()
    {
        $data = Users::select();
        return json(['code' => 200,'msg' => '查询成功','data'=> $data]);

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $user = new Users;
        $result = $user->save($data);

        if ($result) {
            $data = [
                $data,
            ];
            return json(['code' => 200,'msg' => '添加成功','data'=> $data]);
        }else{
              // 使用think自带异常类抛出异常
        throw new \think\Exception('添加删除', 411);

        }
    }

   /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();
        $user = new Users;
            $result = $user->where('id', $id)->update($data);
            if ($result) {
                $data = [
                    $data,
                ];
            return json(['code' => 200,'msg' => '修改成功','data'=> $data]);
            }

        
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = Users::where('id', $id)->find();
        $result2 =Users::where('id', $id)->delete();

        if ($result2) {
            $data = [
                $data,
            ];
            return json(['code' => 200,'msg' => '删除成功','data'=> $data]);
        }
    }
}
